package com.gr.smartedubackend.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.constant.CommonConstant;
import com.gr.smartedubackend.model.dto.notice.NoticeQueryRequest;
import com.gr.smartedubackend.model.entity.Notice;
import com.gr.smartedubackend.service.NoticeService;
import com.gr.smartedubackend.mapper.NoticeMapper;
import com.gr.smartedubackend.utils.ThrowUtils;
import org.springframework.stereotype.Service;

/**
* @author 尘轩
* @description 针对表【notice】的数据库操作Service实现
* @createDate 2025-07-03 10:27:48
*/
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
    implements NoticeService{

    @Override
    public QueryWrapper<Notice> getQueryWrapper(NoticeQueryRequest noticeQueryRequest) {

        ThrowUtils.throwIf(noticeQueryRequest == null, ErrorCode.PARAMS_ERROR);

        String title = noticeQueryRequest.getTitle();
        String keyword = noticeQueryRequest.getKeyword();
        Integer status = noticeQueryRequest.getStatus();
        String publisherName = noticeQueryRequest.getPublisherName();
        String sortField = noticeQueryRequest.getSortField();
        String sortOrder = noticeQueryRequest.getSortOrder();

        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(StrUtil.isNotEmpty(title), "title", title);
        queryWrapper.like(StrUtil.isNotEmpty(keyword), "summary", keyword);
        queryWrapper.like(StrUtil.isNotEmpty(keyword), "content", keyword);
        queryWrapper.eq(ObjectUtil.isNotEmpty(status), "status", status);
        queryWrapper.like(StrUtil.isNotEmpty(publisherName), "publisherName", publisherName);
        if (StrUtil.isNotEmpty(sortOrder) && StrUtil.isNotEmpty(sortField)) {
            queryWrapper.orderBy(true, sortOrder.equals("ascend"), sortField);
        } else {
            queryWrapper.orderByDesc(CommonConstant.CREATE_TIME);
        }

        return queryWrapper;
    }
}




